<?php
/**
 * Class that operate on table 'categories'. Database Sqlite.
 *
 * @author: Dragan Milchevski
 * @date: 2012-06-23 13:23
 */
class CategoriesSqliteDAO implements CategoriesDAO{

	/**
	 * Get Domain object by primry key
	 *
	 * @param String $id primary key
	 * @return CategoriesSqlite 
	 */
	public function load($id){
		$sql = 'SELECT * FROM categories WHERE categoryID = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($id);
		return $this->getRow($sqlQuery);
	}

	/**
	 * Get all records from table
	 */
	public function queryAll(){
		$sql = 'SELECT * FROM categories';
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	/**
	 * @ Monica
	 * Get all records from table and return a specific column
	 */
	public function queryAllByColumn($categoryID){
		$sql = 'SELECT '. $categoryID  .' FROM categories';
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	
	/**
	 * Get all records from table ordered by field
	 *
	 * @param $orderColumn column name
	 */
	public function queryAllOrderBy($orderColumn){
		$sql = 'SELECT * FROM categories ORDER BY '.$orderColumn;
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	/**
 	 * Delete record from table
 	 * @param categorie primary key
 	 */
	public function delete($categoryID){
		$sql = 'DELETE FROM categories WHERE categoryID = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($categoryID);
		return $this->executeUpdate($sqlQuery);
	}
	
	/**
 	 * Insert record to table
 	 *
 	 * @param CategoriesSqlite categorie
 	 */
	public function insert($category){
		$sql = 'INSERT INTO categories (category) VALUES (?)';
		$sqlQuery = new SqlQuery($sql);
		
		$sqlQuery->set($category->getCategory());

		$id = $this->executeInsert($sqlQuery);	
		$category->setId($id);
		return $id;
	}
	
	/**
	 * Insert record to table
	 *
	 * @param CategoriesSqlite filescategories
	 */
	public function insertFilesCategories($file_id,$category_id){
		$sql = 'INSERT OR IGNORE INTO filescategories (fileID,categoryID) VALUES (?,?)';
		$sqlQuery = new SqlQuery($sql);
	
		$sqlQuery->setNumber($file_id);
		$sqlQuery->setNumber($category_id);
	
		$id = $this->executeInsert($sqlQuery);
		return $id;
	}
	
	/**
 	 * Update record in table
 	 *
 	 * @param CategoriesSqlite categorie
 	 */
	public function update($category){
		$sql = 'UPDATE categories SET category = ? WHERE categoryID = ?';
		$sqlQuery = new SqlQuery($sql);
		
		$sqlQuery->set($category->getCategory());

		$sqlQuery->setNumber($category->getId());
		return $this->executeUpdate($sqlQuery);
	}

	/**
 	 * Delete all rows
 	 */
	public function clean(){
		$sql = 'DELETE FROM categories';
		$sqlQuery = new SqlQuery($sql);
		return $this->executeUpdate($sqlQuery);
	}

	public function queryByCategory($value){
		$sql = 'SELECT * FROM categories WHERE category = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}
	
	public function queryByDocument($value){
		//$sql = 'SELECT a.categoryID, a.category FROM categories a, filescategories b WHERE a.categoryID = b.categoryID AND b.fileID = ?';
		$sql = 'SELECT * FROM categories WHERE categoryID = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}
	
	/*
	 * Quering cathegories using Collate Nocase
	 */
	public function queryAllColateNocase(){
		$sql = 'SELECT categoryID,category FROM categories ORDER BY category COLLATE NOCASE ASC';
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	/*
	 * Quering number of cathegories 
	*/
	public function queryNumberOfChategories(){
		$sql = 'SELECT count(*) as num FROM categories';
		$sqlQuery = new SqlQuery($sql);
		return $this->querySingleResult($sqlQuery);
	}



	public function deleteByCategory($value){
		$sql = 'DELETE FROM categories WHERE text = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}
	

	

	
	/**
	 * Read row
	 *
	 * @return CategoriesSqlite 
	 */
	protected function readRow($row){
		$category = new Category();
		
		$category->setId($row['categoryID']);
		$category->setCategory($row['category']);
		//$category->setDocuments(DAOFactory::getLibraryDAO()->queryByCategory($row['categoryID']));

		return $category;
	}
	
	protected function getList($sqlQuery){
		//echo "query=".$sqlQuery."<br>";
		$tab = QueryExecutor::execute($sqlQuery);
		$ret = array();
		for($i=0;$i<count($tab);$i++){
			$ret[$i] = $this->readRow($tab[$i]);
		}
		return $ret;
	}
	
	/**
	 * Get row
	 *
	 * @return CategoriesSqlite 
	 */
	protected function getRow($sqlQuery){
		$tab = QueryExecutor::execute($sqlQuery);
		if(count($tab)==0){
			return null;
		}
		return $this->readRow($tab[0]);		
	}
	
	/**
	 * Execute sql query
	 */
	protected function execute($sqlQuery){
		return QueryExecutor::execute($sqlQuery);
	}
	
		
	/**
	 * Execute sql query
	 */
	protected function executeUpdate($sqlQuery){
		return QueryExecutor::executeUpdate($sqlQuery);
	}

	/**
	 * Query for one row and one column
	 */
	protected function querySingleResult($sqlQuery){
		return QueryExecutor::queryForString($sqlQuery);
	}

	/**
	 * Insert row to table
	 */
	protected function executeInsert($sqlQuery){
		return QueryExecutor::executeInsert($sqlQuery);
	}
}
?>
